//
//  DMMessageImageModel.m
//  HDMainWindow
//
//  Created by apple on 2017/5/8.
//  Copyright © 2017年 com.devin.definesys. All rights reserved.
//

#import "DMMessageImageModel.h"
#import "LLMessageThumbnailManager.h"
#import "DMMessageImageCell.h"
#import "UIImage+LLExt.h"

static NSMutableDictionary<NSString *, UIImage *> *tmpImageDict ;

@implementation DMMessageImageModel

+(void)initialize {
    if (self == [DMMessageImageModel class]) {
        tmpImageDict = [NSMutableDictionary dictionary];
    }
}

//FIXME: 环信有时候会出现DownloadStatus==Success,但文件获取为空的情况
- (UIImage *)fullImage {
//    if (self.messageBodyType == kLLMessageBodyTypeImage) {
//        EMImageMessageBody *imgMessageBody = (EMImageMessageBody *)self.sdk_message.body;
//        if (_fromMe || imgMessageBody.downloadStatus == EMDownloadStatusSuccessed) {
//            UIImage *fullImage = [UIImage imageWithContentsOfFile:imgMessageBody.localPath];
//            return fullImage;
//        }
//    }
    
    return nil;
}

- (UIImage *)thumbnailImage {
    if (!_thumbnailImage) {
//        _thumbnailImage = [[LLMessageThumbnailManager sharedManager] thumbnailForMessageModel:self];
        if (_thumbnailImage)
            return _thumbnailImage;
        
        UIImage *thumbnailImage;
        BOOL needSaveToCache = NO;
        BOOL needSaveToDisk = NO;
        BOOL needSaveToTemp = NO;
        
        UIImage *fullImage = [UIImage imageWithContentsOfFile:self.fileLocalPath];
        _thumbnailImageSize = [DMMessageImageCell thumbnailSize:fullImage.size];
        thumbnailImage = [fullImage resizeImageToSize:self.thumbnailImageSize opaque:YES scale:0];
                            needSaveToCache = YES;
                            needSaveToDisk = YES;
        
//        switch (self.modelType) {
//            case kLLMessageBodyTypeImage:{
//                EMImageMessageBody *imgMessageBody = (EMImageMessageBody *)self.sdk_message.body;
//                
//                self.thumbnailImageSize = [LLMessageImageCell thumbnailSize:imgMessageBody.size];
//                if (_fromMe || imgMessageBody.downloadStatus == EMDownloadStatusSuccessed) {
//                    UIImage *fullImage = [UIImage imageWithContentsOfFile:imgMessageBody.localPath];
//                    _thumbnailImageSize = [LLMessageImageCell thumbnailSize:fullImage.size];
//                    thumbnailImage = [fullImage resizeImageToSize:self.thumbnailImageSize opaque:YES scale:0];
//                    
//                    needSaveToCache = YES;
//                    needSaveToDisk = YES;
//                }else if (imgMessageBody.thumbnailDownloadStatus == EMDownloadStatusSuccessed) {
//                    UIImage *image = [UIImage imageWithContentsOfFile:imgMessageBody.thumbnailLocalPath];
//                    _thumbnailImageSize = [LLMessageImageCell thumbnailSize:image.size];
//                    thumbnailImage = [image resizeImageToSize:self.thumbnailImageSize opaque:YES scale:0];
//                    
//                    needSaveToTemp = YES;
//                }
//                //FIXME:对于特殊图，比如超长、超宽、超小图，应该做特殊处理
//                //调用该方法createWithImageInRect后，VM：raster data内存没有变化
//                //以后再解决这个问题
//                //                if (_thumbnailImageSize.height > 2 * IMAGE_MAX_SIZE) {
//                //                    _thumbnailImage = [_thumbnailImage createWithImageInRect:CGRectMake(0, (_thumbnailImageSize.height - IMAGE_MAX_SIZE) / 2 * _thumbnailImage.scale, _thumbnailImageSize.width * _thumbnailImage.scale, IMAGE_MAX_SIZE * _thumbnailImage.scale)];
//                //                }else if (_thumbnailImageSize.width > 2 * IMAGE_MAX_SIZE) {
//                //                    _thumbnailImage = [_thumbnailImage createWithImageInRect:CGRectMake((_thumbnailImageSize.width - IMAGE_MAX_SIZE)/2 * _thumbnailImage.scale, 0, IMAGE_MAX_SIZE * _thumbnailImage.scale, _thumbnailImageSize.height * _thumbnailImage.scale)];
//                //                }
//                
//                break;
//            }
//            case kLLMessageBodyTypeVideo:{
//                EMVideoMessageBody *videoMessageBody = (EMVideoMessageBody *)self.sdk_message.body;
//                
//                if (_fromMe || videoMessageBody.downloadStatus == EMDownloadStatusSuccessed ) {
//                    UIImage *image = [LLUtils getVideoThumbnailImage:videoMessageBody.localPath];
//                    thumbnailImage = [image resizeImageToSize:self.thumbnailImageSize];
//                    
//                    needSaveToCache = YES;
//                    needSaveToDisk = YES;
//                }else if (videoMessageBody.thumbnailDownloadStatus == EMDownloadStatusSuccessed) {
//                    UIImage *image = [[UIImage alloc] initWithContentsOfFile:videoMessageBody.thumbnailLocalPath];
//                    thumbnailImage = [image resizeImageToSize:self.thumbnailImageSize];
//                    
//                    needSaveToTemp = YES;
//                }
//                
//                break;
//            }
//            case kLLMessageBodyTypeLocation: {
//                if (self.defaultSnapshot)
//                    return nil;
//                
//                EMFileMessageBody *body = (EMFileMessageBody *)self.sdk_message.body;
//                if (_fromMe || body.downloadStatus == EMDownloadStatusSuccessed) {
//                    NSData *data = [NSData dataWithContentsOfFile:body.localPath];
//                    thumbnailImage = [UIImage imageWithData:data scale:_snapshotScale];
//                    
//                    needSaveToCache = YES;
//                    needSaveToDisk = NO;
//                }
//                
//                break;
//            }
//                
//            default:
//                break;
//        }
        
        if (thumbnailImage) {
            if (needSaveToTemp) {
                tmpImageDict[self.messageId] = thumbnailImage;
            }else if (needSaveToCache) {
                if (self.messageId) {
                    tmpImageDict[self.messageId] = nil;
                    [[LLMessageThumbnailManager sharedManager] addThumbnailForMessageModel:self thumbnail:thumbnailImage toDisk:needSaveToDisk];
                }
            }
        }
        
        _thumbnailImage = thumbnailImage;
    }
    
    return _thumbnailImage;
}

@end
